package com.longteng.lesson2.controller;


import com.google.common.collect.Lists;
import com.longteng.lesson2.domain.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.AbstractHandlerMethodMapping;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/rest")
@SessionAttributes(types = {User.class})
public class RestTestController {

    @RequestMapping("/hi")
    public String sayHi(){

        return "hello springboot";
    }
    @PostMapping("/postMethod")
    public String postMethod(){
        return "hello postMethod";
    }
    @GetMapping("/getMethod")
    public String getMethod(){
        return "hello getMethod";
    }
    @RequestMapping(value = "/requestPostMethod",method = RequestMethod.PUT)
    public String requestPostMethod(){
        return "hello requestPostMethod";
    }

    @RequestMapping("/returnUser")
    public User getUser(){
        //在视图解析层面对User做序列化处理，jackson
        User user=new User();
        user.setAge(18);
        return user;
    }
    @Autowired
    ApplicationContext applicationContext;
    @RequestMapping("/getMapPath")
    public List path(){
        List<String> exclusions = Lists.newArrayList();
        AbstractHandlerMethodMapping<RequestMappingInfo> objHandlerMethodMapping  =
                (AbstractHandlerMethodMapping<RequestMappingInfo>) applicationContext.getBean("requestMappingHandlerMapping");
        Map<RequestMappingInfo, HandlerMethod> mapRet =
                objHandlerMethodMapping.getHandlerMethods();
        for (RequestMappingInfo requestMappingInfo : mapRet.keySet()) {
            Set set = requestMappingInfo.getPatternsCondition().getPatterns();
            Iterator iterator = set.iterator();
            while (iterator.hasNext()) {
                exclusions.add(iterator.next().toString());
            }
        }
        return exclusions;
    }
    @RequestMapping(value = "/getUser1",params = {"age=20"},headers = {"Host=127.0.0.1:8888"})
    public User getUser1(){
        //在视图解析层面对User做序列化处理，jackson
        User user=new User();
        user.setAge(18);
        return user;
    }
    @RequestMapping(value = "/test")
    public ModelAndView test(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("index");
        User user=new User();
        user.setName("tt");
        modelAndView.addObject("user",user);
        return modelAndView;
    }
    @RequestMapping(value = "s")
    public @ResponseBody Object s(HttpSession httpSession,String n)
    {
        Object o =httpSession.getAttribute(n);
       return o;
    }
//    @GetMapping(value = "/{email}/{name}")
//    public String restful(@PathVariable("name") String aaa,@PathVariable("email")String bbb){
//        return aaa+bbb;
//    }

    @RequestMapping(value = "/getCookieValue",method = RequestMethod.GET)
    public String getCookieValue(@CookieValue(value = "study") String cookie){
        return cookie;
    }

}
